package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.PageBean;
import com.itheima.dto.EmpQueryDto;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.service.EmpService;
import com.itheima.vo.EmpVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    EmpMapper empMapper;

    //分页查询所有员工
    @Override
    public PageBean pageQuery(EmpQueryDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<EmpVo> page = empMapper.query(dto);
        long total = page.getTotal();
        List<EmpVo> list = page.getResult();
        PageBean pageBean = new PageBean(total, list);
        return pageBean;
    }

    //批量删除员工
    @Override
    public void deleteEmp(Integer[] ids) {
        for (Integer id : ids) {
            empMapper.deleteById(id);
        }
    }

    //新增员工
    @Override
    public void addEmp(@RequestBody Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        emp.setCreateTime(LocalDateTime.now());
        empMapper.insert(emp);
    }
}
